Imports System.IO
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Collections


''' <summary>
''' Generate SQL connector for DataAccess
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[cinj]	16/06/2006	Created
''' </history>
Public Class ProviderFactory

    Protected Friend Enum EnumProviders
        SQLClient
    End Enum

    Private Shared _provider As EnumProviders
    Private Shared _IsProviderFetched As Boolean
    Private Shared _ConnString As String

    Private Sub New()
    End Sub

    Protected Friend Shared Function GetCommand() As IDbCommand

        Select Case GetProvider()
            Case EnumProviders.SQLClient
                Return New SqlCommand
        End Select
        Return Nothing
    End Function

    Protected Friend Shared Function GetCommand( _
    ByVal strCmdText As String, _
    ByVal cmdType As CommandType) As IDbCommand

        Dim cmd As IDbCommand = GetCommand()
        PrepareCommand(cmd, strCmdText, cmdType)
        Return cmd

    End Function

    Protected Friend Shared Function GetCommand( _
ByVal strCmdText As String, _
ByVal ParameterArray As IDbDataParameter()) As IDbCommand

        Dim cmd As IDbCommand = GetCommand()
        Dim strParams As String = ""
        Dim i, count As Int16
        count = ParameterArray.Length - 1
        For i = 0 To count
            Select Case GetProvider()
                Case EnumProviders.SQLClient
                    strParams = strParams & ParameterArray(i).ParameterName & IIf(ParameterArray(i).Direction = ParameterDirection.Output Or ParameterArray(i).Direction = ParameterDirection.InputOutput, " Output", "") & ", "
            End Select
            cmd.Parameters.Add(ParameterArray(i))
        Next
        cmd.CommandText = PrepareCommandText(strCmdText, strParams.Remove(strParams.Length - 2, 2))
        cmd.CommandType = CommandType.Text

        Return cmd
    End Function

    Protected Friend Shared Function GetConnection() As IDbConnection

        Select Case GetProvider()
            Case EnumProviders.SQLClient
                Return New SqlConnection
        End Select
        Return Nothing
    End Function

    Protected Friend Shared Function GetConnection(ByVal strConnString As String) As IDbConnection
        Dim con As IDbConnection = GetConnection()
        con.ConnectionString = strConnString
        con.Open()
        Return con
    End Function

    Protected Friend Shared Function GetAdapter() As IDbDataAdapter
        Select Case GetProvider()
            Case EnumProviders.SQLClient
                Return New SqlDataAdapter
        End Select
        Return Nothing
    End Function

    Protected Friend Shared Function GetAdapter( _
    ByVal strCommandText As String, _
    ByVal cmdType As CommandType, _
    ByVal strConnString As String _
    ) As IDbDataAdapter
        Select Case GetProvider()
            Case EnumProviders.SQLClient
                Return New SqlDataAdapter(PrepareCommandText(strCommandText, cmdType), strConnString)
        End Select
        Return Nothing
    End Function

    Protected Friend Shared Function GetAdapter( _
     ByVal strCommandText As String, _
     ByVal strConnString As String _
     ) As IDbDataAdapter
        Select Case GetProvider()
            Case EnumProviders.SQLClient
                Return New SqlDataAdapter(strCommandText, strConnString)
        End Select
        Return Nothing
    End Function

    Protected Friend Shared Function GetParameter() As IDbDataParameter
        Select Case GetProvider()
            Case EnumProviders.SQLClient
                Return New SqlParameter
        End Select
        Return Nothing
    End Function

    Protected Friend Shared Function GetParameter( _
     ByVal paramName As String, _
     ByVal paramDirection As ParameterDirection, _
     ByVal paramValue As Object, _
    ByVal paramtype As DbType) As IDbDataParameter
        Dim param As IDbDataParameter = Nothing
        Select Case GetProvider()
            Case EnumProviders.SQLClient
                param = New SqlParameter(paramName, paramtype)
                If paramDirection = ParameterDirection.Input Or paramDirection = ParameterDirection.InputOutput Then
                    param.Value = paramValue
                End If
                param.Direction = paramDirection
        End Select
        Return param
    End Function

    Protected Friend Shared Function GetParameter( _
     ByVal paramName As String, _
     ByVal paramValue As Object _
    ) As IDbDataParameter
        Dim param As IDbDataParameter = Nothing
        Select Case GetProvider()
            Case EnumProviders.SQLClient
                param = New SqlParameter(paramName, paramValue)
        End Select
        Return param
    End Function

    Private Shared Sub PrepareCommand _
    (ByRef cmd As IDbCommand, _
    ByVal strSQL As String, _
    ByVal cmdType As CommandType)

        cmd.CommandType = CommandType.Text
        cmd.CommandText = strSQL

    End Sub

    Protected Friend Shared Function PrepareCommandText _
    (ByVal strSQL As String, ByVal strParams As String) As String

        Select Case GetProvider()
            Case EnumProviders.SQLClient
                Return "Exec " & strSQL & " " & strParams
        End Select
        Return String.Empty
    End Function

    Protected Friend Shared Function GetProvider() As EnumProviders
        If Not _IsProviderFetched Then
            '            Dim Idt As IDictionary = CType(ConfigurationSettings.GetConfig("ConnectionInfo"), IDictionary)
            '           Return CType(Idt("Provider"), EnumProviders)
            _IsProviderFetched = True
            _provider = EnumProviders.SQLClient
        End If
        Return _provider
    End Function
End Class
